/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;
import java.math.*;
/**
 *
 * @author DucThien
 */
public class pagination {
    private int totalOnThis;
    private int page;
    private int limit;
    private int num_links;
    private String url;
    private String text;
    private String text_first;
    private String text_last;
    private String text_next;
    private String text_prev;
    private String style_links;
    private String style_results;
        
    public pagination(){
        totalOnThis = 0;
        page = 1;
        limit = 6;
        num_links = 10;
        url = "";
        text = "Showing {start} to {end} of {total} ({pages} Pages)";
        text_first = "|&lt;";
        text_last = "&gt;|";
        text_next = "&gt;";
        text_prev = "&lt;";
        style_links = "pagination-deal";
        style_results = "results";
    }
    public String renderPagination(){
        int total = this.getTotalOnThis();
        int page = 0;
        int start = 0;
        int end = 0 ;
        if (this.getPage() < 1) {
                page = 1;
        } else {
                page = this.getPage();
        }
        int limit = 0;
        if (this.getLimit() < 0) {
                limit = 10;
        } else {
                limit = this.getLimit();
        }

        int num_links = this.getNum_links();
        int num_pages = (int)Math.ceil(((float)total / (float)limit));

        String output = "";

        if (page > 1) {
                output += " <a href='" ;
                output += this.getUrl().replace("{page}","1");
                output += "'>";
                output += this.getText_first();
                output += "</a>";
                output += "<a href='";
                int prePage = page - 1;
                output += this.getUrl().replace("{page}", String.valueOf(prePage)); 
                output += "'>"; 
                output += this.getText_prev(); 
                output += "</a> ";
        }
        if (num_pages > 1) {
                if (num_pages <= num_links) {
                        start = 1;
                        end = num_pages;
                } else {
                        start = (int) (page - Math.floor((float)num_links / 2));
                        end = (int) (page + Math.floor((float)num_links / 2));

                        if (start < 1) {
                                end += Math.abs(start) + 1;
                                start = 1;
                        }

                        if (end > num_pages) {
                                start -= (end - num_pages);
                                end = num_pages;
                        }
                }

                if (start > 1) {
                        output += " .... ";
                }

                for (int i = start; i <= end; i++) {
                        if (page == i) {
                                output += " <b>" + i + "</b> ";
                        } else {
                                output += "<a href='" ;
                                output += this.getUrl().replace("{page}", String.valueOf(i));
                                output += "'>";
                                output += i + "</a> ";
                        }	
                }

                if (end < num_pages) {
                        output += " .... ";
                }
        }

        if (page < num_pages) {
            output += "<a href='" ;
            output += this.getUrl().replace("{page}", String.valueOf(page + 1));
            output += "'>";
            output += this.getText_next() + "</a> ";
            output += "<a href='" ;
            output += this.getUrl().replace("{page}", String.valueOf(num_pages));
            output += "'>";
            output += this.getText_last() + "</a> ";
            
        }

        
        int valueStart , valueEnd;
        if(total > 0){
            valueStart = ((page - 1) * limit) + 1;
        }else{
            valueStart = 0;
        }
        if((((page - 1) * limit) > (total - limit))){
            valueEnd = total;
        }else{
            valueEnd =(((page - 1) * limit) + limit);
        }
         
        
        
        this.getText().replace("start",String.valueOf(valueStart) );
        this.getText().replace("end", String.valueOf(valueEnd));
        this.getText().replace("total",String.valueOf(total) );
        this.getText().replace("pages",String.valueOf(num_pages) );
        return (!"".equals(output) ? "<div class='" + this.getStyle_links() + "'>" + output + "</div>" : "") ;//+ "<div class='" + this.style_results + "'>" + this.text +"</div>";
    }

    /**
     * @return the totalOnThis
     */
    public int getTotalOnThis() {
        return totalOnThis;
    }

    /**
     * @param totalOnThis the totalOnThis to set
     */
    public void setTotalOnThis(int totalOnThis) {
        this.totalOnThis = totalOnThis;
    }

    /**
     * @return the page
     */
    public int getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(int page) {
        this.page = page;
    }

    /**
     * @return the limit
     */
    public int getLimit() {
        return limit;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * @return the num_links
     */
    public int getNum_links() {
        return num_links;
    }

    /**
     * @param num_links the num_links to set
     */
    public void setNum_links(int num_links) {
        this.num_links = num_links;
    }

    /**
     * @return the url
     */
    public String getUrl() {
        return url;
    }

    /**
     * @param url the url to set
     */
    public void setUrl(String url) {
        this.url = url;
    }

    /**
     * @return the text
     */
    public String getText() {
        return text;
    }

    /**
     * @param text the text to set
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     * @return the text_first
     */
    public String getText_first() {
        return text_first;
    }

    /**
     * @param text_first the text_first to set
     */
    public void setText_first(String text_first) {
        this.text_first = text_first;
    }

    /**
     * @return the text_last
     */
    public String getText_last() {
        return text_last;
    }

    /**
     * @param text_last the text_last to set
     */
    public void setText_last(String text_last) {
        this.text_last = text_last;
    }

    /**
     * @return the text_next
     */
    public String getText_next() {
        return text_next;
    }

    /**
     * @param text_next the text_next to set
     */
    public void setText_next(String text_next) {
        this.text_next = text_next;
    }

    /**
     * @return the text_prev
     */
    public String getText_prev() {
        return text_prev;
    }

    /**
     * @param text_prev the text_prev to set
     */
    public void setText_prev(String text_prev) {
        this.text_prev = text_prev;
    }

    /**
     * @return the style_links
     */
    public String getStyle_links() {
        return style_links;
    }

    /**
     * @param style_links the style_links to set
     */
    public void setStyle_links(String style_links) {
        this.style_links = style_links;
    }

    /**
     * @return the style_results
     */
    public String getStyle_results() {
        return style_results;
    }

    /**
     * @param style_results the style_results to set
     */
    public void setStyle_results(String style_results) {
        this.style_results = style_results;
    }
   
}
